package com.xindong.paking.controller;


import com.xindong.paking.entity.TbRole;
import com.xindong.paking.entity.vo.TbRoleVo;
import com.xindong.paking.service.TbRoleService;
import com.xindong.paking.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangshengce
 * @since 2023-01-08
 */
@Api(tags = "权限管理模块")
@RestController
@RequestMapping("/role")
public class TbRoleController {

    @Autowired
    private TbRoleService tbRoleService;

    @ApiOperation("新增管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "name", value = "名字"),
            @ApiImplicitParam(name = "email", value = "邮箱")
    })
    @PostMapping("/add")
    public Result add(@RequestBody TbRoleVo tbRoleVo) {
        TbRole tbRole = new TbRole();
        BeanUtils.copyProperties(tbRoleVo, tbRole);
        return tbRoleService.addAdmin(tbRole);
    }


    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小")
    })
    @GetMapping("list")
    public Result list(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        return tbRoleService.list(page, pageSize);
    }



    @ApiOperation("用户更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "name", value = "名字"),
            @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "role", value = "权限(0:管理员,1:超级管理员)")
    })
    @PutMapping("update")
    public Result update(@RequestBody TbRole tbRole) {
        return tbRoleService.updateRole(tbRole);
    }



    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id")
    })
    @DeleteMapping("delete")
    public Result delete(@RequestParam("id") Integer id) {
        return tbRoleService.deleteRole(id);
    }


}

